import 'dart:async';

import 'package:flutter/material.dart';
import 'package:test/RouteObserverUtil.dart';
import 'package:test/Tabs.dart';
import 'package:test/video/video.dart';
import 'package:test/homePage.dart';
// import 'package:flutter_bugly/flutter_bugly.dart';
import 'package:mobile_device_identifier_ohos/mobile_device_identifier_ohos.dart';
import 'package:test/path_provider/path_provider.dart';
import 'package:test/webview.dart';
import 'package:test/sqflite/main.dart';

/// 使用 flutter 异常上报
void main() {
  runApp(MyApp());
  // FlutterBugly.postCatchedException(() {
  //   // 如果需要 ensureInitialized，请在这里运行。
  //   // WidgetsFlutterBinding.ensureInitialized();
  //   runApp(MyApp());
  //   FlutterBugly.init(
  //     ohosAppId: "4cfad2b8b6",
  //     deviceId: "2121212112",
  //     appKey: "f34c1b09-fc28-4d99-b9e9-0372da33c722",
  //   );
  // }, debugUpload: true);
}
//bugly日志

@pragma('vm:entry-point')
void secondMain() => runApp(secondPage());

class MyApp extends StatefulWidget {
  final bool isDetached;
  const MyApp({this.isDetached = false, Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  //  MyApp({super.key});
  final RouteObserverUtil util = RouteObserverUtil();
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  Map routes = {
    "/home": (context) => Taps(), //tabIndex设置默认显示tab页
    "/video": (context) => video(),
    "/webview": (context) => WebViewExample(),
    "/sqlite": (context) => SqfliteExampleApp(),
  };
  @override
  Widget build(BuildContext context) {
    if (widget.isDetached) {
      Widget resultWidget = const SizedBox.shrink();
      return resultWidget;
    }
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'welcome to  flutter',
      initialRoute: "/home",
      onGenerateRoute: (settings) {
        final String? name = settings.name;
        final Function? pagecontentBulider = routes[name];
        if (pagecontentBulider != null) {
          if (settings.arguments != null) {
            final Route route = MaterialPageRoute(
                builder: (context) =>
                    pagecontentBulider(context, arguments: settings.arguments));
            return route;
          } else {
            final Route route = MaterialPageRoute(
                builder: (context) => pagecontentBulider(context));
            return route;
          }
        }
        return null;
      },
      navigatorObservers: <NavigatorObserver>[util.routeObserver],
    );
  }

  @override
  void dispose() {
    super.dispose();
  }
}

class secondPage extends StatelessWidget {
  secondPage({super.key});
  final RouteObserverUtil util = RouteObserverUtil();
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: Center(
          child: const Text('Flutter Demo Home Page'),
        ));
  }
}
